package com.hsd.survey.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsd.core.base.dao.IBaseDAO;
import com.hsd.core.base.service.impl.BaseService;
import com.hsd.core.page.Finder;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.survey.dao.ISurveyCommonDao;
import com.hsd.survey.model.HsdDataResult;
import com.hsd.survey.model.HsdModelAnswer;
import com.hsd.survey.model.HsdModelQuestion;
import com.hsd.survey.model.HsdModelSurvey;
import com.hsd.survey.service.ISurveyCommonService;

/**
 * <p>
 * 类描述：问卷公用服务实现类
 * <p>
 * 创建人：Abdi
 * <p>
 * 创建时间：Nov 16, 2013 2:56:47 PM
 */
@Service
@SuppressWarnings("unchecked")
public class SurveyCommonServiceImpl extends BaseService implements ISurveyCommonService{
	@Autowired
	private ISurveyCommonDao surveyDao;
	
	/**
	 * 加载问卷模板列表
	 * @param pageParams
	 * @param surveyname
	 * @return
	 */
	@Override
	public Pagination loadSurveyList(PageParams pageParams,String name,String status) {
		Finder finder = Finder.create("from HsdModelSurvey");
		finder.append(" where 1=1");
		if(!StringUtils.isBlank(status)){
			finder.append(" and status=:status");
			finder.setParam("status", Integer.parseInt(status));
		}
		if(!StringUtils.isBlank(name)){
			finder.append(" and name like:name");
			finder.setParam("name", "%" + name + "%");
		}
		Pagination page = surveyDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}
	
	/**
	 * 加载调研结果列表
	 * @param pageParams
	 * @return
	 */
	@Override
	public Pagination loadResearchList(PageParams pageParams,String surveyname,String answername){
		Finder finder = Finder.create("select r,s");
		finder.append(" from HsdDataResearch r,HsdModelSurvey s");
		finder.append(" where s.surveyuuid = r.surveyuuid");
		if(!StringUtils.isBlank(answername)){
			finder.append(" and r.name like:answername");
			finder.setParam("answername", "%" + answername + "%");
		}
		if(!StringUtils.isBlank(surveyname)){
			finder.append(" and s.name like:surveyname");
			finder.setParam("surveyname", "%" + surveyname + "%");
		}
		Pagination page = surveyDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}
	
	/**
	 * 获取问卷模板数据
	 * @param surveyuuid
	 * @return
	 */
	@Override
	public JSONObject loadSurveyData(String surveyuuid,String researchuuid,boolean isLoadAnswer){
		// 1 加载问卷信息
		Map queryMap = new HashMap();
		queryMap.put("surveyuuid", surveyuuid);
		HsdModelSurvey survey = (HsdModelSurvey)surveyDao.getUniqueObject(HsdModelSurvey.class, queryMap);
		
		// 2 获取问题集合
		queryMap.clear();
		queryMap.put("surveyuuid", surveyuuid);
		queryMap.put("isgridchildquestion", 0);
		List<HsdModelQuestion> questionList = surveyDao.findList(HsdModelQuestion.class, queryMap, "sortindex");
		
		// 3 加载问题其他部件
		JSONArray questionsArray = new JSONArray();
		for(HsdModelQuestion modelQuestion : questionList){
			// 问题参数
			JSONObject oneQuestion = JSONObject.fromObject(modelQuestion);
			
			// 加载子问题
			queryMap.clear();
			queryMap.put("fatherquestionuuid", modelQuestion.getQuestionuuid());
			queryMap.put("isgridchildquestion", 1);
			queryMap.put("surveyuuid", surveyuuid);
			List<HsdModelQuestion> childQuestionList = surveyDao.findList(HsdModelQuestion.class, queryMap, "sortindex");
			oneQuestion.put("questions", JSONArray.fromObject(childQuestionList));
			
			// 加载答案模板
			queryMap.clear();
			queryMap.put("surveyuuid", surveyuuid);
			queryMap.put("questionuuid", modelQuestion.getQuestionuuid());
			List<HsdModelAnswer> answerList = surveyDao.findList(HsdModelAnswer.class, queryMap, "sortindex");
			oneQuestion.put("answers", JSONArray.fromObject(answerList));
			
			// 加载答案结果
			if(isLoadAnswer){
				queryMap.clear();
				queryMap.put("surveyuuid", surveyuuid);
				queryMap.put("questionuuid", modelQuestion.getQuestionuuid());
				queryMap.put("researchuuid", researchuuid);
				List<HsdDataResult> resultlist = surveyDao.findList(HsdDataResult.class,queryMap);
				oneQuestion.put("results",JSONArray.fromObject(resultlist));
			}
			
			questionsArray.add(oneQuestion);
		}
		
		JSONObject result = new JSONObject();
		result.put("survey", JSONObject.fromObject(survey));
		result.put("questionlist", questionsArray);
		result.put("error", 0);
		result.put("msg", null);
		return result;
	}
	
	/**
	 * 加载逻辑编辑数据
	 * @param surveyuuid
	 * @return
	 */
	@Override
	public JSONObject loadLogicData(String surveyuuid){
		JSONObject result = loadSurveyData(surveyuuid, null, false);
		JSONArray logicArray = new JSONArray();
		result.put("logiclist", logicArray);
		return result;
	}
	
	@Override
	public IBaseDAO getBaseDao() {
		return this.surveyDao;
	}
}
